<?php
/**
 * @file
 * @package mileage
 * @version $Id$
**/

if (!defined('XOOPS_ROOT_PATH')) {
	exit;
}

require_once MILEAGE_TRUST_PATH . '/class/AbstractEditAction.class.php';

/**
 * Mileage_Admin_ChargeAction
**/
class Mileage_Admin_ChargeAction extends Mileage_AbstractEditAction
{
	/**
	 * _getId
	 * 
	 * @param   void
	 * 
	 * @return  int
	**/
	protected function _getId()
	{
		return $this->mRoot->mContext->mRequest->getRequest('mile_id');
	}

	/**
	 * &_getHandler
	 * 
	 * @param   void
	 * 
	 * @return  Mileage_MileHandler
	**/
	protected function &_getHandler()
	{
		$handler =& $this->mAsset->getObject('handler', 'mile');
		return $handler;
	}

	/**
	 * _setupActionForm
	 * 
	 * @param   void
	 * 
	 * @return  void
	**/
	protected function _setupActionForm()
	{
		$this->mActionForm =& $this->mAsset->getObject('form', 'mile', TRUE, 'edit');
		$this->mActionForm->prepare();
	}

	/**
	 * executeViewInput
	 * 
	 * @param   XCube_RenderTarget  &$render
	 * 
	 * @return  void
	**/
	public function executeViewInput(&$render)
	{
		$render->setTemplateName('charge.html');
		$render->setAttribute('actionForm', $this->mActionForm);
		$render->setAttribute('object', $this->mObject);
		$render->setAttribute('dirname', $this->mAsset->mDirname);

		$config_handler =& xoops_gethandler('config');
		$xoopsConfigUser =& $config_handler->getConfigsByDirname('user');
		$render->setAttribute('maxuname', intval($xoopsConfigUser['maxuname']));
	}

	/**
	* executeViewSuccess
	* 
	* @param   XCube_RenderTarget  &$render
	* 
	* @return  void
	**/
	public function executeViewSuccess(/*** XCube_RenderTarget ***/ &$render)
	{
		$dirname = $this->mAsset->mDirname;
		list($name, $toUid) = explode('UID:', $this->mObject->get('name'));

		if ($toUid > 0) {
			// TODO :-P
			$db =& $this->mRoot->mController->getDB();
			$sql = sprintf(
					'INSERT INTO `%s` (`point`, `mile_id`, `uid`, `reg_unixtime`) VALUES (%d, %d, %d, %d)',
						$db->prefix($dirname . '_rec'),
						intval($this->mObject->get('point')),
						intval($this->mObject->get('mile_id')),
						intval($toUid),
						time()
				);

			if ($db->query($sql)) {
				$this->mRoot->mController->executeForward('./index.php?action=RecList');
			} else {
				$this->mRoot->mController->executeRedirect('./index.php?action=Charge', 1, 
					_MD_MILEAGE_ERROR_DBUPDATE_FAILED . '(CHARGE ERROR)');
			}
		} else {
			$this->mRoot->mController->executeRedirect('./index.php?action=Charge', 1, 
				_MD_MILEAGE_ERROR_DBUPDATE_FAILED . '(CHARGE ERROR : UID)');
		}
	}

	/**
	 * executeViewError
	 * 
	 * @param   XCube_RenderTarget  &$render
	 * 
	 * @return  void
	**/
	public function executeViewError(/*** XCube_RenderTarget ***/ &$render)
	{
		$this->mRoot->mController->executeRedirect('./index.php?action=Charge', 1, _MD_MILEAGE_ERROR_DBUPDATE_FAILED);
	}

	/**
	 * executeViewCancel
	 * 
	 * @param   XCube_RenderTarget  &$render
	 * 
	 * @return  void
	**/
	public function executeViewCancel(/*** XCube_RenderTarget ***/ &$render)
	{
		$this->mRoot->mController->executeForward('./index.php?action=MileList');
	}

}
